/*   Copyright 2010 Jonathan Wagner
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
package com.alienos.collide.bounds;

import com.alienos.collide.math.Vector3;



public class BoundingSphere extends BoundingVolume {

    private final Vector3 center;
    private final float radius;
   
    public BoundingSphere(float radius) {
        this.radius = radius;
        this.center = Vector3.ZERO;
    }

    public BoundingSphere(Vector3 center, float radius) {
        this.radius = radius;
        this.center = center;
    }

    public Vector3 getCenter() {
        return center;
    }

    public float getRadius() {
        return radius;
    }

    @Override
    public boolean intersects(BoundingSphere sphere) {
        // Calculate distance between sphere centers
        Vector3 d = getCenter().subtract(sphere.getCenter());
        double d2 = d.dot(d);
        // They intersect if square distance is less than squared sum of radii
        float radiusSum = getRadius() + sphere.getRadius();
        return d2 <= radiusSum * radiusSum;
    }
    


    @Override
    public boolean intersects(BoundingBox box) {
        return intersectSphereAABB(this, box);
    }

    @Override
    public boolean intersects(BoundingVolume v) {
        if( null != v) {
            return v.intersects(this);
        }
        else {
            return false;
        }
    }

	@Override
	public boolean contains(Vector3 point) {
	    return center.distanceSquared(point) < (radius * radius);
	}

}
